package com.lftea.dao.impl;

import java.sql.Date;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.lftea.dao.IProductDAO;
import com.lftea.dao.IStorekHouseDAO;
import com.lftea.dao.IStockPileDAO;
import com.lftea.po.Product;
import com.lftea.po.StoreHouse;
import com.lftea.po.StockPile;

@Repository("stockPileDAOImpl")
public class StockPileDAOImpl implements IStockPileDAO {

	@Resource(name = "jdbcTemplate")
	private JdbcTemplate jdbcTemplate;

	@Resource(name = "productDAOImpl")
	private IProductDAO productDAOImpl;

	@Resource(name = "storeHouseDAOImpl")
	private IStorekHouseDAO storeHouseDAOImpl;

	public List<StockPile> findAll() {
		String sql = "select * from StockPile";
		return jdbcTemplate.query(sql, new StockPileMapper());
	}

	public StockPile findById(long stockPileId) {
		String sql = "select * from StockPile where StockPile_ID=?";
		return (StockPile) jdbcTemplate.query(sql,
				new Object[] { stockPileId }, new StockPileMapper());
	}

	public List<StockPile> findByStoreHouseId(long storeHouseId) {
		String sql = "select * from StockPile where StoreHouse_ID=?";
		return jdbcTemplate.query(sql, new Object[] { storeHouseId },
				new StockPileMapper());
	}

	public long findTotalByStoreHouseId(long storeHouseId) {
		String sql = "select count(*) from StockPile where StoreHouse_ID=?";
		return jdbcTemplate.queryForLong(sql, new Object[] { storeHouseId });
	}

	public void remove(StockPile stockPile) {
		long stockPileId = stockPile.getStockPileId();
		String sql = "delete * from StockPile where StockPile_ID=?";
		jdbcTemplate.update(sql, new Object[] { stockPileId });
	}

	public void save(StockPile stockPile) {
		long stockPileId = stockPile.getStockPileId();
		Product product = stockPile.getProduct();
		long productId = product.getId();
		float price = stockPile.getPrice();
		float qty = stockPile.getQty();
		StoreHouse storeHouse = stockPile.getStoreHouse();
		long storeHouseId = storeHouse.getId();
		Date firstEnterDate = stockPile.getFirstEnterDate();
		Date lastEnterDate = stockPile.getLastLeaveDate();

		String sql = "select count(*) from StockPile where StockPile_ID=?";
		int result = jdbcTemplate
				.queryForInt(sql, new Object[] { stockPileId });
		Object[] params = null;
		if (result > 0) {
			sql = "update StockPile set ProductId=?,Price=?,Qty=?,StoreHouse_ID=?,FirstEnterDate=?,LastEnterDate=? where StockPile_ID=?";
			params = new Object[] { productId, price, qty, storeHouseId,
					firstEnterDate, lastEnterDate, stockPileId };
		} else {
			sql = "insert into StockPile(ProductId,Price,Qty,StoreHouse_ID,FirstEnterDate,LastEnterDate) values(?,?,?,?,?)";
			params = new Object[] { productId, price, qty, storeHouseId,
					firstEnterDate, lastEnterDate };
		}
		jdbcTemplate.update(sql, params);
	}

	public void removeByStoreHouseId(long storeHouseId) {
		String sql = "delete StoreHouse where StoreHouse_ID=?";
		jdbcTemplate.update(sql, new Object[] { storeHouseId });
	}

	private final class StockPileMapper implements RowMapper<StockPile> {

		public StockPile mapRow(ResultSet rs, int rowNum) throws SQLException {
			StockPile stockPile = new StockPile();
			stockPile.setStockPileId(rs.getLong("StockPile_ID"));
			long productId = rs.getLong("Product_ID");
			Product product = productDAOImpl.findById(productId);
			stockPile.setProduct(product);
			long storeHouseId = rs.getLong("StoreHouse_ID");
			StoreHouse storeHouse = storeHouseDAOImpl.findById(storeHouseId);
			stockPile.setStoreHouse(storeHouse);
			stockPile.setPrice(rs.getFloat("price"));
			stockPile.setQty(rs.getFloat("qty"));
			stockPile.setFirstEnterDate(rs.getDate("firstEnterDate"));
			stockPile.setLastLeaveDate(rs.getDate("lastLeaveDate"));
			return stockPile;
		}

	}

}
